﻿<%@ WebHandler Language="C#" Class="getSpot" %>

using System;
using System.Web;
using System.Collections.Generic;
using WuDada.Core.Post.Domain;
using Newtonsoft.Json;
using WuDada.Core.Post.Service;
using WuDada.Core.Post;

public class getSpot : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        //取得DataTable
        IList<PostVO> caseList = QueryCases(context);
        //將DataTable轉成JSON字串
        string json = JsonConvert.SerializeObject(caseList, Formatting.Indented);
        context.Response.Write(json);
    }

    /// <summary>
    /// 抓地點
    /// </summary>
    /// <returns></returns>
    private IList<PostVO> QueryCases(HttpContext context)
    {
        Common.Logging.ILog log = Common.Logging.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        
        PostFactory postFactory = new PostFactory(); ;
        IPostService postService = postFactory.GetPostService();
        string city = context.Request.Form["city"];
        string area = context.Request.Form["area"];
        string road = context.Request.Form["road"];
        string address = (city + area + road); 
        string caseKeyWord = context.Request.Form["caseKeyWord"];
        string priceMin = context.Request.Form["priceMin"];
        string priceMax = context.Request.Form["priceMax"];
        string startPriceMin = context.Request.Form["startPriceMin"];
        string startPriceMax = context.Request.Form["startPriceMax"];

        IList<PostVO> caseList = new List<PostVO>();

        try
        {
            Dictionary<string, string> conditions = new Dictionary<string, string>();
            conditions.Add("NodeId", "8");
            conditions.Add("Flag", "1");
            conditions.Add("WithOutSubPost", "1");
            conditions.Add("City", city);
            conditions.Add("Area", area);
            conditions.Add("Road", road);
            conditions.Add("PriceMin", priceMin);
            conditions.Add("PriceMax", priceMax);
            conditions.Add("StartPriceMin", startPriceMin);
            conditions.Add("StartPriceMax", startPriceMax);
            conditions.Add("CaseKeyWord", caseKeyWord);

            caseList = postService.GetPostList(conditions);

            if (caseList == null || caseList.Count == 0)
            {
                caseList = new List<PostVO>();
            }

            ////定位點
            IList<string> latLng = GoogleApiHelper.GetLatLng(address);
            if (latLng != null && latLng.Count == 2)
            {
                PostVO caseData = new PostVO();
                caseData.Lat = latLng[0].Substring(0, 10);
                caseData.Lng = latLng[1].Substring(0, 10);

                caseList.Insert(0, caseData);
            }
        }
        catch (Exception ex)
        {
            log.Error(ex);
            log.Info(ex);
            log.Debug(ex);
        }

        return caseList;
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}